﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Entity;

namespace PM.Site.Admin
{
    public partial class OrderForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                this.Loading();
                //if (Session["tradeDbKey"] != null && Session["tradeDbKey"].ToString() != string.Empty)
                //{
                //    this.Loading(Session["tradeDbKey"].ToString());
                //}
            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }

        }

        private void Loading()
        {
            if (!Page.IsPostBack)
            {

            }
        }

        private void Loading(string tradeKeyDb)
        {
            if (!Page.IsPostBack)
            {
                //char[] seps = { ',' };
                //string[] tradeValues = tradeKeyDb.Split(seps);

                //Trade trade = TradeServices.GetTradeById(tradeValues[0], tradeValues[1], tradeValues[2], tradeValues[3]);

                //txtTradeDate.Text = trade.TradeDate;
                //txtTradeTime.Text = trade.TradeTime;
                //ddlBuySell.SelectedValue = trade.BuySell;
                //txtExecutionId.Text = trade.ExecutionId;
                //txtCompanyId.Text = trade.CompanyId;
                //txtMainType.Text = trade.MainType;
                //txtSubType.Text = trade.SubType;
                //txtQuanity.Text = trade.Quantity.ToString();
                //txtPrice.Text = trade.Price.ToString();
                //txtLotType.Text = trade.LotType.ToString();
                //txtTaxFlag.Text = trade.TaxFlag.ToString();
                //txtBrokerIdBuy.Text = trade.BrokerIdBuy;
                //txtBrokerIdSell.Text = trade.BrokerIdSell;
                //txtBuyingContractNo.Text = trade.BuyingContractNo.ToString();
                //txtSellingContractNo.Text = trade.SellingContractNo.ToString();
                //txtClientPrefix.Text = trade.ClientPrefix.ToString();
                //txtClientSuffix.Text = trade.ClientSuffix;
                //txtJointAcNo.Text = trade.JointAcNo.ToString();
                //txtParticipantId.Text = trade.ParticipantId;
                //txtForeignFlag.Text = trade.ForeignFlag.ToString();
                //txtBrokerage.Text = trade.Brokerage.ToString();
                //txtCdsFees.Text = trade.CdsFees.ToString();
                //txtCseFees.Text = trade.CseFees.ToString();
                //txtStampDuty.Text = trade.StampDuty.ToString();
                //txtSecCess.Text = trade.SecCess.ToString();
                //txtBtt.Text = trade.Btt.ToString();
                //txtInterestOnDebt.Text = trade.InterestOnDebt.ToString();
                //txtOrderId.Text = trade.OrderId;
                //txtTranId.Text = trade.TranId;
                //txtStatus.Text = trade.Status.ToString();
                //txtGovernmentCess.Text = trade.GovernmentCess.ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int intTemp = 0;
            double dblTemp = 0;
            Order order = new Order();

            intTemp = 0;
            if (Int32.TryParse(txtOrderId.Text, out intTemp))
            {
                order.OrderId = intTemp;
            }
            
            order.ClienId = txtClientId.Text;
            order.SecurityId = txtSecurityId.Text;
            
            intTemp = 0;
            if (Int32.TryParse(txtOriginalQty.Text, out intTemp))
            {
                order.OriginalQty = intTemp;
            }

            dblTemp = 0;
            if (Double.TryParse(txtPrice.Text, out dblTemp))
            {
                order.Price = dblTemp;
            }
            order.OrderDateTime = txtOrderDateTime.Text;
            order.OrderType = txtOrderType.Text;
            order.BrokerRef = txtBrokerRef.Text;
            order.Tif = txtTIF.Text;

            intTemp = 0;
            if (Int32.TryParse(txtTIFDays.Text, out intTemp))
            {
                order.TifDays = intTemp;
            }
            order.LotType = txtLotType.Text;
            order.OriginalOrderId = txtOriginalOrderId.Text;
            order.TranId = txtTranId.Text;
            order.BuySell = ddlBuySell.SelectedValue;

            intTemp = 0;
            if (Int32.TryParse(txtMinFillQty.Text, out intTemp))
            {
                order.MinFillQty= intTemp;
            }
            intTemp = 0;
            if (Int32.TryParse(txtForeignBroker.Text, out intTemp))
            {
                order.ForeignBroker = intTemp;
            }

            order.TradeId = txtTradeId.Text;
            order.BrokerOffice = txtBrokerOffice.Text;


            OrderServices.Add(order);



        }
    }
}
